﻿using Hrm.Core;
using Hrm.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Hrm.Web.Areas.Studio.Controllers
{
    public class TeamMembersController : Controller
    {
        ITeamMemberService teamMemberService;

        public TeamMembersController(ITeamMemberService _teamMemberService)
        {
            this.teamMemberService = _teamMemberService;
        }

        public ActionResult Index()
        {
            var resp = this.teamMemberService.GetAllAbledAndOrderByCreatetime();
            return View(resp);
        }

        public ActionResult Create()
        {
            var model = new TeamMenber();
            return View(model);
        }

        [HttpPost]
        public ActionResult Create(TeamMenber req)
        {
            this.teamMemberService.Insert(req);
            return RedirectToAction("Index");
        }

        public ActionResult Edit(int id)
        {
            var model = this.teamMemberService.GetById(id);
            return View(model);
        }

        [HttpPost]
        public ActionResult Edit(TeamMenber req)
        {
            var model = this.teamMemberService.GetById(req.Id);
            if (TryUpdateModel(model, "", new[] { "ChineseName", "EnglishName", "Type", "UserImg", "Company", "Position", "Description", "OrderNum" }))
            {
                this.teamMemberService.Update(model);
                return RedirectToAction("Index");
            }
            return View();
        }

        public ActionResult Details(int id)
        {
            return View();
        }

        public ActionResult Delete(int id)
        {
            var result = this.teamMemberService.Delete(id);
            return RedirectToAction("Index");
        }
    }
}